<?php

$image = preg_replace('/^(s?f|ht)tps?:\/\/[^\/]+/i', '', (string) $_GET['image']);
$size	= GetImageSize($image);
$mime	= $size['mime'];
$temp = 'temp/';

if(!file_exists('temp'))
	mkdir('temp', 0755);
	
$dir = explode('/', $image);
$fileName = $dir[count($dir) - 1];
$dir[count($dir) - 1] = "";
$dir = join('/', $dir);

$thumbDir = $dir . 'thumb';

if(!is_writable($dir))
	chmod($dir, 0755);

if(file_exists($thumbDir . '/' . $fileName))
{
	$file = fopen($thumbDir . '/' . $fileName, 'r');
	$data = fread($file, filesize($thumbDir . '/' . $fileName));
	
	header("Content-type: $mime");
	header('Content-Length: ' . strlen($data));
	
	echo $data;
	exit();
}

if(!file_exists($thumbDir))
	mkdir($thumbDir, 0755);

if (substr($mime, 0, 6) != 'image/')
{
	header('HTTP/1.1 400 Bad Request');
	exit();
}

$width = $size[0];
$height	= $size[1];

$maxWidth = (isset($_GET['width'])) ? (int) $_GET['width'] : 0;
$maxHeight = (isset($_GET['height'])) ? (int) $_GET['height'] : 0;

$offsetX = 0;
$offsetY = 0;

$cropRatio = array(33, 27);

$ratioComputed = $width / $height;
$cropRatioComputed = (float) $cropRatio[0] / (float) $cropRatio[1];

if ($ratioComputed < $cropRatioComputed)
{
	$origHeight	= $height;
	$height	= $width / $cropRatioComputed;
	$offsetY = ($origHeight - $height) / 2;
}
else if($ratioComputed > $cropRatioComputed)
{
	$origWidth = $width;
	$width = $height * $cropRatioComputed;
	$offsetX = ($origWidth - $width) / 2;
}

$xRatio	= $maxWidth / $width;
$yRatio	= $maxHeight / $height;

if ($xRatio * $height < $maxHeight)
{
	$tnHeight = ceil($xRatio * $height);
	$tnWidth = $maxWidth;
}
else
{
	$tnWidth = ceil($yRatio * $width);
 	$tnHeight = $maxHeight;
}

$quality = 90;

$resized = $thumbDir . '/' . $fileName;

$dst = imagecreatetruecolor($tnWidth, $tnHeight);

switch ($size['mime'])
{
	case 'image/gif':
		$creationFunction	= 'ImageCreateFromGif';
		$outputFunction		= 'ImagePng';
		$mime				= 'image/png';
		$doSharpen			= FALSE;
		$quality			= round(10 - ($quality / 10));
	break;
	
	case 'image/x-png':
	case 'image/png':
		$creationFunction	= 'ImageCreateFromPng';
		$outputFunction		= 'ImagePng';
		$doSharpen			= FALSE;
		$quality			= round(10 - ($quality / 10));
	break;
	
	default:
		$creationFunction	= 'ImageCreateFromJpeg';
		$outputFunction	 	= 'ImageJpeg';
		$doSharpen			= TRUE;
	break;
}

$src = $creationFunction($image);

ImageCopyResampled($dst, $src, 0, 0, $offsetX, $offsetY, $tnWidth, $tnHeight, $width, $height);

$outputFunction($dst, $resized, $quality);

chmod($resized, 0755);
ob_start();
$outputFunction($dst, null, $quality);
$data	= ob_get_contents();
ob_end_clean();

ImageDestroy($src);
ImageDestroy($dst);

$lastModifiedString	= gmdate('D, d M Y H:i:s', filemtime($resized)) . ' GMT';
$etag				= md5($data);

doConditionalGet($etag, $lastModifiedString);

header("Content-type: $mime");
header('Content-Length: ' . strlen($data));
echo $data;

ImageDestroy($src);
ImageDestroy($dst);

function doConditionalGet($etag, $lastModified)
{
	header("Last-Modified: $lastModified");
	header("ETag: \"{$etag}\"");
		
	$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
		stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : 
		false;
	
	$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
		stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
		false;
	
	if (!$if_modified_since && !$if_none_match)
		return;
	
	if ($if_none_match && $if_none_match != $etag && $if_none_match != '"' . $etag . '"')
		return;
	
	if ($if_modified_since && $if_modified_since != $lastModified)
		return;
	
	header('HTTP/1.1 304 Not Modified');
	exit();
} 

?>